<template>
  <el-dialog
    title="充值"
    :visible.sync="visible"
    width="400px"
  >
    <el-form ref="form" :model="form" :rules="formRules">
      <el-form-item prop="amount" label="充值金额">
        <el-input-number v-model="form.amount" :min="1" :precision="2" controls-position="right" style="width:50%" />
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="submitting" @click="onSubmit" >确认</el-button>
      <el-button type="default" @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>


<script>
export default {
  data() {
    return {
      visible: false,
      submitting: false,
      form: {
        accountId: null,
        userId: null,
        amount: undefined
      },
      formRules: {
        accountId: [{required: false}],
        userId: [{required: true}],
        amount: [{required: true, trigger: 'submit', message: '请输入金额'}]
      }
    };
  },
  methods: {
    open(options) {
      this.visible = true;
      this.form.accountId = options.accountId;
      this.form.userId = options.user.id;
      this.form.amount = undefined;
      this.options = options;
    },
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return;

        this.submitting = true;
        const ret = await this.$http.post('/api/user/wallet/user-org-balance-account/deposit', this.form);
        if (ret.success) {
          this.visible = false;
          this.$nextTick(() => {
            this.$message.success('充值成功');
            this.options.onSuccess();
          });
        } else {
          this.$message.error(ret.message);
        }
        this.submitting = false;
      });
    }
  }
}

</script>
